/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.flan.stock.adapter;

import java.util.ArrayList;
import java.util.List;

import com.flan.stock.R;
import com.flan.stock.bean.Stock;
import com.flan.stock.view.HVListView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 沪深A股列表数据适配器
 * @author flan
 * @since  2016年4月18日
 */
public class StockRankDataAdapter extends BaseAdapter{
	
	private List<Stock> stockList;
	private LayoutInflater mInflater;
	private HVListView hvlistView;
	
	public StockRankDataAdapter(Context context, HVListView hvlistView) {
		super();
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);;
		this.hvlistView = hvlistView;
	}
	
	public void setData(List<Stock> stockList){
		this.stockList = stockList;
	}
	
	@Override
	public int getCount() {
		if(stockList != null){
			return stockList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(stockList != null){
			return stockList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_hvlistview, parent,false);
            holder.tv_name = (TextView) convertView.findViewById(R.id.name);
            holder.tv_code = (TextView) convertView.findViewById(R.id.code);
            holder.tv_now = (TextView) convertView.findViewById(R.id.item1);
            holder.tv_low = (TextView) convertView.findViewById(R.id.item2);
            holder.tv_high = (TextView) convertView.findViewById(R.id.item3);
            holder.tv_open = (TextView) convertView.findViewById(R.id.item4);
            holder.tv_close = (TextView) convertView.findViewById(R.id.item5);
            holder.tv_volume = (TextView) convertView.findViewById(R.id.item6);
            holder.tv_amount = (TextView) convertView.findViewById(R.id.item7);
            convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(stockList != null && stockList.size() > 0){
			Stock stock = stockList.get(position);
			
			holder.tv_name.setText(stock.getName());
			holder.tv_code.setText(stock.getCode());
			holder.tv_now.setText(stock.getNowPrice()+"");
			holder.tv_low.setText(stock.getlPrice()+"");
			holder.tv_high.setText(stock.gethPrice()+"");
			holder.tv_open.setText(stock.getOpenPrice()+"");
			holder.tv_close.setText(stock.getClosePrice()+"");
			holder.tv_volume.setText(stock.getTraVolume()+"");
			holder.tv_amount.setText(stock.getTraAmount()+"");
		}
		

        /*for (int i = 0; i < 7; i++) {
            ((TextView) convertView.findViewById(R.id.item1 + i)).setText("数据" + position + "行" + (i + 2) + "列");
        }*/

        //校正（处理同时上下和左右滚动出现错位情况）
        /*View child = ((ViewGroup) convertView).getChildAt(1);
        int headscrollx = hvlistView.getHeadScrollX();
        if (child.getScrollX() != headscrollx) {
            child.scrollTo(hvlistView.getHeadScrollX(), 0);
        }*/
        return convertView;
	}
	
	/**
	 * 用于保存ListView中重用的item视图的引用
	 * @author flan
	 * @since  2016年4月19日
	 */
	class ViewHolder{
		public TextView tv_name,tv_code,tv_now,tv_low,tv_high,tv_open,tv_close,tv_volume,tv_amount;
	}

}

















